package com.frogs.ucenter.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.component.cache.Cache;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserMessageSmsVerifyCode;
import com.frogs.ucenter.service.SmsService;

@Controller
public class SmsController extends AbsController {

	private static Logger log = LoggerFactory.getLogger(SmsController.class);

	private static final String SEND_VERIFY = "verify/send";
	private static final String CHECK_VERIFY = "verify/check";
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	@Resource
	private SmsService smsService;
	@Resource(name="xmemcached")
	private Cache cache;
	
	/**
	 * 校验验证码
	 * @param request
	 * @return
	 */
	@RequestMapping(CHECK_VERIFY)
	public JSONObject checkVerify(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			JSONObject result = new JSONObject();
			JSONObject response = new JSONObject();
			String phone = requestInfo.getParameter("phone",null);
			String verifyCode = requestInfo.getParameter("verifyCode",null);
			
			if(!StringUtil.isPhoneNumber(phone) || StringUtil.isEmpty(phone)) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "手机号码格式错误!");
				result.put("response", response);
				return result;
			}
			
			Boolean checkResult = smsService.checkVerifyCode(phone, verifyCode, UserMessageSmsVerifyCode.SendOperationEnum.BIND.getValue());
			if(checkResult == Boolean.FALSE) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "校验失败!");
				result.put("response", response);
				return result;
			}
			response.put("code", ApiResultEnum.SUCCESS.getId());
			response.put("tip", "校验成功");
			result.put("response", response);
			return result;
		} catch (Exception e) {
			log.error("func[checkVerify] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 发送验证码
	 * @param request
	 * @return
	 */
	@RequestMapping(SEND_VERIFY)
	public JSONObject sendVerify(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			JSONObject result = new JSONObject();
			JSONObject response = new JSONObject();
			String phone = requestInfo.getParameter("phone",null);
			
			if(!StringUtil.isPhoneNumber(phone) || StringUtil.isEmpty(phone)) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "手机号码格式错误!");
				result.put("response", response);
				return result;
			}
			
			Boolean checkResult = smsService.sendVerifyByphone(phone, UserMessageSmsVerifyCode.SendOperationEnum.BIND.getValue(), 60*2);
			if(checkResult == Boolean.FALSE) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "校验失败!");
				result.put("response", response);
				return result;
			}
			response.put("code", ApiResultEnum.SUCCESS.getId());
			response.put("tip", "校验成功");
			result.put("response", response);
			return result;
		} catch (Exception e) {
			log.error("func[sendVerify] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
}
